import java.net.*;

import java.io.*;

/**
 * 发送消息的结构定义
 *
 * @author Administrator
 */
public class NetJavaMsg {

    private int totalLen; //数据的长度

    private int id;  //唯一的ID

    private byte[] data; //消息内容

//本地参数，为简化起见，不发送

    private SocketAddress recvRespAdd;  //发送者接收应答的地址

    private SocketAddress destAdd;  //接收者地址

    private int sendCount=0; //发送次数

    private long lastSendTime; //最后一次发送的时间

    /**
     * Instantiates a new Net java msg.
     *
     * @param id   //唯一的序号
     * @param data //数据内容
     */
    public NetJavaMsg(int id,byte[] data){

        this.id=id;

        this.data=data;

        totalLen=4+4+data.length;

    }

    /**
     * Instantiates a new Net java msg.
     *
     * @param udpData //将受到的udp数据解析为NetJavaMsg对象
     */
    public NetJavaMsg(byte[] udpData){

        try{

            ByteArrayInputStream bins=new ByteArrayInputStream(udpData);

            DataInputStream dins=new DataInputStream(bins);

            this.totalLen=dins.readInt();

            this.id=dins.readInt();

            this.data=new byte[totalLen-4-4];

            dins.readFully(data);

        }catch(Exception e){

            e.printStackTrace();

        }

    }

    /**
     * To byte byte [ ].
     *
     * @return the byte [ ]
     */
    public byte[] toByte(){

        try{

            ByteArrayOutputStream bous=new ByteArrayOutputStream();

            DataOutputStream dous=new DataOutputStream(bous);

            dous.writeInt(totalLen);

            dous.writeInt(id);

            dous.write(data);

            dous.flush();

            return bous.toByteArray();

        }catch(Exception e){

            e.printStackTrace();

        }

        return null;

    }

    @Override

    public String toString() {

// TODO Auto-generated method stub

        return "id:"+id+"  content"+new String(data)+"  totalLen"+totalLen+" sengerAdd:"+recvRespAdd+"  destAdd:"+destAdd;

    }

    /**
     * Gets total len.
     *
     * @return the total len
     */
    public int getTotalLen() {

        return totalLen;

    }

    /**
     * Sets total len.
     *
     * @param totalLen the total len
     */
    public void setTotalLen(int totalLen) {

        this.totalLen = totalLen;

    }

    /**
     * Gets id.
     *
     * @return the id
     */
    public int getId() {

        return id;

    }

    /**
     * Sets id.
     *
     * @param id the id
     */
    public void setId(int id) {

        this.id = id;

    }

    /**
     * Get data byte [ ].
     *
     * @return the byte [ ]
     */
    public byte[] getData() {

        return data;

    }

    /**
     * Sets data.
     *
     * @param data the data
     */
    public void setData(byte[] data) {

        this.data = data;

    }

    /**
     * Gets recv resp add.
     *
     * @return the recv resp add
     */
    public SocketAddress getRecvRespAdd() {

        return recvRespAdd;

    }

    /**
     * Sets recv resp add.
     *
     * @param recvRespAdd the recv resp add
     */
    public void setRecvRespAdd(SocketAddress recvRespAdd) {

        this.recvRespAdd = recvRespAdd;

    }

    /**
     * Gets dest add.
     *
     * @return the dest add
     */
    public SocketAddress getDestAdd() {

        return destAdd;

    }

    /**
     * Sets dest add.
     *
     * @param destAdd the dest add
     */
    public void setDestAdd(SocketAddress destAdd) {

        this.destAdd = destAdd;

    }

    /**
     * Gets send count.
     *
     * @return the send count
     */
    public int getSendCount() {

        return sendCount;

    }

    /**
     * Sets send count.
     *
     * @param sendCount the send count
     */
    public void setSendCount(int sendCount) {

        this.sendCount = sendCount;

    }

    /**
     * Gets last send time.
     *
     * @return the last send time
     */
    public long getLastSendTime() {

        return lastSendTime;

    }

    /**
     * Sets last send time.
     *
     * @param lastSendTime the last send time
     */
    public void setLastSendTime(long lastSendTime) {

        this.lastSendTime = lastSendTime;

    }

}





